<?php
/**
 * 
 * @author bqd
 *
 */
class HasSingleImageBehavior extends ModelBehavior {
	var $options = array();
    function setup(&$model, $settings = array()) {
        $default = array(
			'image_field'	=> 'image',
		);

		if (!isset($this->__settings[$model->alias])) {
			$this->__settings[$model->alias] = $default;
		}

		$this->__settings[$model->alias] = am($this->__settings[$model->alias], $settings);
    }
	
	function deleteImage(&$model, $id) {
		$data[$model->alias] = array(
			"id" => $id 
		);
		$model->set($data);
		
		// Read image information 
		$oldImage = $model->field($this->__settings[$model->alias]['image_field']);
		
		// delete this field, set it to empty
		$model->saveField($this->__settings[$model->alias]['image_field'], '');
		
		return $oldImage;
	}
    
    function beforeDelete(&$model, $cascade) {
        // Lấy tên file ảnh
        $imageField = $this->__settings[$model->alias]['image_field'];
        //debug($model->name);
        $data = $model->find('first', array(
            'conditions'    => array(
                $model->name.'.id'    => $model->id
            ),
            'fields'    => array($imageField),
            'contain' => array()
        ));
        
        if (!isset($model->config)) {
			$model->config = $model->loadConfig();
		}
		
        if (in_array('Tree', $model->Behaviors->_attached)) {
           
            if ($model->config['Category']['image']['show']) {
    			if (is_file($model->config['Category']['image']['path'].$data[$model->name][$imageField])) {
    				@unlink($model->config['Category']['image']['path'].$data[$model->name][$imageField]);
    			}
    			
    			// Xóa thumbnail
    			if ($model->config['Category']['thumbnail']['thumbnail']) {
    				// Xử lý để lấy tên thumbnail
    				$ext = strrchr($data[$model->name][$imageField], ".");
    				$pos = strrpos($data[$model->name][$imageField], ".");
    				$filename = substr($data[$model->name][$imageField],0,$pos);
    				$thumbnail_name = $filename."_thumbnail".$ext;
    				if (is_file($model->config['Category']['thumbnail']['path'].$thumbnail_name)) {
    					@unlink($model->config['Category']['thumbnail']['path'].$thumbnail_name);
    				}
    			}
    		}
        } else {
            if ($model->config[$model->name]['image']['show']) {
    			if (is_file($model->config[$model->name]['image']['path'].$data[$model->name][$imageField])) {
    				@unlink($model->config[$model->name]['image']['path'].$data[$model->name][$imageField]);
    			}
    			
    			// Xóa thumbnail
    			if ($model->config[$model->name]['thumbnail']['thumbnail']) {
    				// Xử lý để lấy tên thumbnail
    				$ext = strrchr($data[$model->name][$imageField], ".");
    				$pos = strrpos($data[$model->name][$imageField], ".");
    				$filename = substr($data[$model->name][$imageField],0,$pos);
    				$thumbnail_name = $filename."_thumbnail".$ext;
    				if (is_file($model->config[$model->name]['thumbnail']['path'].$thumbnail_name)) {
    					@unlink($model->config[$model->name]['thumbnail']['path'].$thumbnail_name);
    				}
    			}
    		}
        }
        
		
        return true;
    }
}
?>